﻿using SIG.Infrastructure.Configs;
using SIG.Infrastructure.Logging;
using SIG.Model.Identity;
using SIG.Service.Identity;
using System.Net;
using System.Web;
using System.Web.Mvc;


namespace SIG.Web.Areas.Admin.Controllers
{
    [Authorize]
    public class RoleController : BaseController
    {
       

        IRoleService _RoleService;
       
        public RoleController(IRoleService RoleService)
        {
            _RoleService = RoleService;
        }

        // GET: Role
        public ActionResult Index()
        {
           

            var roles = _RoleService.GetAll();
            return View(roles);
        }



        // GET: Roles/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Roles/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Create([Bind(Include = "Id,RoleName,Description")] Role role)
        {
            if (ModelState.IsValid)
            {
                _RoleService.Create(role);

                return Json(true, JsonRequestBehavior.DenyGet);
            }

            return Json(false, JsonRequestBehavior.DenyGet);
        }

        // GET: Roles/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Role role = _RoleService.GetById(id);
            if (role == null)
            {
                return HttpNotFound();
            }
            return View(role);
        }

        // POST: Roles/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Edit([Bind(Include = "Id,RoleName,Description")] Role role)
        {
            if (ModelState.IsValid)
            {
                if (role.IsSys)
                {
                    AR.Status = Model.Enums.Status.Warning;
                    AR.Message = "预留角色，不可以修改！";
                    return Json(AR, JsonRequestBehavior.DenyGet);
                }

                _RoleService.Update(role);
                return Json(true, JsonRequestBehavior.DenyGet);
            }
            return Json(false, JsonRequestBehavior.DenyGet);
        }



        // POST: Roles/Delete/5
        [HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {

            Role role = _RoleService.GetById(id);
            if(role.IsSys)
            {
                AR.Status = Model.Enums.Status.Warning;
                AR.Message = "预留角色，不可以删除！";
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            if (role == null)
            {
                AR.Status = Model.Enums.Status.Error;
                AR.Message = "未找到此角色！";
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            _RoleService.Delete(role);
            return Json(AR, JsonRequestBehavior.DenyGet);
           
        }


    }
}